name: packages

on:
  push:
    tags:
      - 'v*.*.*'
  workflow_dispatch:

env:
  DEBEMAIL: jaakko.keranen@iki.fi
  DEBFULLNAME: Jaakko Keränen (skyjake)

jobs:
  build:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: '0'
          submodules: 'recursive'
      - name: install-dependencies
        run: |
          git submodule sync
          sudo apt-get update -qq -y
          sudo apt-get install -y -qq --no-install-recommends cmake libsdl2-dev libssl-dev libpcre3-dev zlib1g-dev libunistring-dev libmpg123-dev debhelper dh-make devscripts fakeroot build-essential python3 python3-pip libharfbuzz-dev libfribidi-dev
          sudo pip3 install git-archive-all
      - name: import-signing-key
        run: |
          mkdir -p ~/.gnupg/
          printf "$GPG_SIGNING_KEY" | base64 --decode > ~/.gnupg/private.key
          gpg --import ~/.gnupg/private.key
        env:
          GPG_SIGNING_KEY: ${{ secrets.GPG_SIGNING_KEY }}
      - name: package-build
        run: |
          export RELEASE_VERSION=`git tag | sort -rV | head -n1 | sed 's/v\(.*\)/\1/'`
          git archive-all --prefix=lagrange-${RELEASE_VERSION}/ lagrange-${RELEASE_VERSION}.tar.gz
          tar -xvzf lagrange-${RELEASE_VERSION}.tar.gz
          ln -s lagrange-${RELEASE_VERSION}.tar.gz lagrange_${RELEASE_VERSION}.orig.tar.gz
          cd lagrange-${RELEASE_VERSION}
          dch -v ${RELEASE_VERSION}-1 "Built by GitHub CI."
          dch -r ""
          debuild
          cd ..
          mkdir -p artifacts
          mv *deb artifacts/
        shell: bash
      - name: plain-build
        run: |
          export RELEASE_VERSION=`git tag | sort -rV | head -n1 | sed 's/v\(.*\)/\1/'`
          mkdir builddir
          cd builddir
          cmake .. -DCMAKE_BUILD_TYPE=Release
          cmake --build .
          mkdir -p ../artifacts/
          tar -czvf ../artifacts/lagrange-${RELEASE_VERSION}-linux-amd64.tar.gz lagrange
          cd ..
      - name: upload-artifact
        uses: actions/upload-artifact@v2
        with:
          name: lagrange_ubuntu-18.04
          path: artifacts
  debian_build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: '0'
          submodules: 'recursive'
      - name: get-repo-name
        run: |
          echo "REPOSITORY_NAME="$(echo $GITHUB_REPOSITORY | awk -F / '{print $2}' | sed -e "s/:refs//") >> $GITHUB_ENV
        shell: bash
      - name: install-dependencies
        run: |
          git submodule sync
      - name: debian_docker
        uses: ./.github/workflows/docker-debian/
      - name: upload-debian-artifact
        uses: actions/upload-artifact@v2
        with:
          name: lagrange_debian-buster
          path: /home/runner/work/${{env.REPOSITORY_NAME}}/${{env.REPOSITORY_NAME}}/artifacts
